﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using QDT.Common;
using QDT.Core.Members;
using QDT.Services.Assignments;
using QDT.Services.Deals;
using QDT.Web.Models;

namespace QDT.Web.Controllers
{
    public partial class AppController
    {

        public JsonResult InvestProject(string router)
        {

            var model = JsonConvert.DeserializeObject<InvestProjectModel>(string.IsNullOrEmpty(router) ? "" : router);

            if (model.money % 1 != 0)
                return Json(new { result = 0, msg = "投资金额需要填入正整数" }, JsonRequestBehavior.AllowGet);

            var str = model.userid.ToString("0") + model.projectid.ToString("0") + model.money.ToString("0") + SecretKey;

            if (!(str).Hash().ToUpper().Equals(model.token.ToUpper()))
            {
                return Json(new { result = "-999", msg = "未知异常" }, JsonRequestBehavior.AllowGet);
            }

            if (model.money % 1 != 0)
                return Json(new { result = 0, msg = "投资金额需要填入正整数" }, JsonRequestBehavior.AllowGet);

            var user = DbProvider.GetByID<User>(model.userid);

            if (user.GroupID != 3)
            {
                return Json(new { result = 0, msg = "金额异常" }, JsonRequestBehavior.AllowGet);
            }

            try
            {
                DbProvider.IsNotSubmit = true;
                var credit = IoC.Resolve<IDealService>().Bid(model.userid, model.projectid, model.money);
                credit.Client = model.client;
                DbProvider.IsNotSubmit = false;
                DbProvider.SaveChanges();
            }
            catch (Exception e)
            {
                return Json(new { result = 0, msg = e.Message }, JsonRequestBehavior.AllowGet);
            }

            return Json(new { result = 1, msg = "投资成功" }, JsonRequestBehavior.AllowGet);
        }


        public JsonResult InvestAssign(string router)
        {
            var model = JsonConvert.DeserializeObject<InvestAssignModel>(string.IsNullOrEmpty(router) ? "" : router);

            if (model.money % 1 != 0)
                return Json(new { result = 0, msg = "投资金额需要填入正整数" }, JsonRequestBehavior.AllowGet);

            var str = model.userid.ToString("0") + model.assignid.ToString("0") + model.money.ToString("0") + SecretKey;

            if (!(str).Hash().ToUpper().Equals(model.token.ToUpper()))
            {
                return Json(new { result = "-999", msg = "未知异常" }, JsonRequestBehavior.AllowGet);
            }

            if (model.money % 1 != 0)
                return Json(new { result = 0, msg = "投资金额需要填入正整数" }, JsonRequestBehavior.AllowGet);

            var user = DbProvider.GetByID<User>(model.userid);

            if (user.GroupID != 3)
            {
                return Json(new { result = 0, msg = "金额异常" }, JsonRequestBehavior.AllowGet);
            }

            try
            {
                DbProvider.IsNotSubmit = true;
                var credit = IoC.Resolve<IAssignmentService>().Bid(model.userid, model.assignid, model.money);
                credit.Client = model.client;
                DbProvider.IsNotSubmit = false;
                DbProvider.SaveChanges();
            }
            catch (Exception e)
            {
                return Json(new { result = 0, msg = e.Message }, JsonRequestBehavior.AllowGet);
            }

            return Json(new { result = 1, msg = "投资成功" }, JsonRequestBehavior.AllowGet);
        }

    }
}
